<template>
  <div class="mlt">
    <div style="margin-bottom: 10px">
      <span>商品名称：</span>
      <el-input
        v-model="form.goods_name"
        placeholder="请输入搜索商品名称"
        style="width: 200px"
      />
      <el-button style="margin-left: 10px" type="primary" @click="init">
        搜索
      </el-button>
      <el-button
        v-permissions="{ permission: ['export:StockBalance'] }"
        @click="Export"
      >
        导出
      </el-button>
      <el-button
        v-permissions="{ permission: ['view:StockBalance'] }"
        type="text"
        @click="exportList"
      >
        查看已导出列表
      </el-button>
    </div>
    <div>
      <el-table :data="tableData" :empty-text="emptyText" style="width: 100%">
        <el-table-column label="商品名称" prop="goods_name" />
        <el-table-column label="规格名称" prop="attr_name" />
        <el-table-column label="剩余库存" prop="total_surplus_qty" />
      </el-table>
      <el-pagination
        v-if="total >= 10"
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      :before-close="handleClose"
      title="导出"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-table :data="exportData" style="width: 100%">
        <el-table-column label="日期" prop="updated_at" />
        <el-table-column label="操作">
          <template slot-scope="scoped">
            <el-button
              v-permissions="{ permission: ['download:StockBalance'] }"
              type="text"
              @click="download(scoped.row)"
            >
              下载
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { stockList, stockExport, stockBalance } from '@/api/commodity'

  export default {
    props: {
      // detailsdata: {
      //   type: Object,
      //   required: true,
      // },
    },
    data() {
      return {
        emptyText: ' ',
        tableData: [],
        list: {},
        form: {
          pagesize: 10,
          page: 1,
          goods_name: '',
        },
        total: '',
        dialogVisible: false,
        exportData: [],
      }
    },
    watch: {},
    mounted() {
      this.init()
    },
    methods: {
      Export() {
        const data = {
          goods_name: this.form.goods_name,
        }
        stockExport(data).then(() => {
          // const url = e.data.base_url + e.data.src
          // window.open(url)
          this.$message({
            message: '已添加导出列表',
            type: 'success',
          })
          this.exportList()
        })
      },
      exportList() {
        stockBalance().then((e) => {
          this.exportData = e.data

          this.dialogVisible = true
        })
      },
      download(e) {
        // const url = 'http://fan.jb.cc/' + e.src
        const url = e.base_url + e.src
        window.open(url)
      },
      init() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        stockList(this.form).then((e) => {
          this.tableData = e.data
          this.total = e.data.total
          loading.close()
          if (e.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
      },
      handleCurrentChange(val) {
        const _this = this
        _this.form.page = val
        this.init()
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done()
          })
          .catch(() => {})
      },
    },
  }
</script>

<style></style>
